package com.zsinfo.guoss.supplier.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.netcenter.NetCenter;
import com.zsinfo.guoss.supplier.MyApplication;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.broadcast.NetBroadcastReceiver;
import com.zsinfo.guoss.supplier.broadcast.NetEvent;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * N_BaseActivity
 * wkh
 */
public abstract class BaseActivity extends AppCompatActivity implements NetEvent {
    public ProgressDialog dialog;
    protected static Context context;
    protected static Activity activity;
    private LinearLayout l_base_layout;
    private LinearLayout net_less_layout;
    private LinearLayout data_less_layout;
    private LinearLayout l_title;
    private TextView n_tv_title, n_left_title;
    protected ImageView n_action;
    private LinearLayout back;
    private Unbinder unbinder;
    private NetBroadcastReceiver myReceiver;
    private Bundle savedInstanceState;


    private void registerBroadcast() {
        myReceiver = new NetBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        filter.setPriority(Integer.MAX_VALUE);
        registerReceiver(myReceiver, filter);
        myReceiver.setNetEvent(this);
    }


    protected NetCenter GetNetCenter() {
        return new NetCenter(this);
    }


    //网络变化时使用
    @Override
    public void onNetChange(int netState) {
        if (netState == 0) {
            net_less_layout.setVisibility(View.VISIBLE);
        } else {
            net_less_layout.setVisibility(View.GONE);
        }
    }

    //activity 没有数据的时候
    protected void SetNoData(boolean HasData) {
        if (HasData) {
            data_less_layout.setVisibility(View.VISIBLE);
        } else {
            data_less_layout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        this.savedInstanceState = savedInstanceState;
        context = getApplicationContext();
        activity = this;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            //全屏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        changeStatusBarTextColor(true);
        setContentView(getContentLayoutRes());
        MyApplication.addToStackList(this);

        registerBroadcast();
        initView();
        initNetData();
    }


    //状态栏颜色

    public void setWindowStatusBarColor(Activity activity, int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(activity.getResources().getColor(colorResId));

                //底部导航栏
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //状态栏字体颜色
    private void changeStatusBarTextColor(boolean isBlack) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            if (isBlack) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//设置状态栏黑色字体
            } else {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
            }
        }
    }

    protected Bundle getThisActivitySavedInstanceState() {
        return this.savedInstanceState;
    }

    /**
     * 获取activity布局
     *
     * @return
     */
    protected abstract int getContentLayoutRes();

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        setContentView(View.inflate(this, layoutResID, null));
    }

    @Override
    public void setContentView(View view) {
        l_base_layout = (LinearLayout) findViewById(R.id.l_base_layout);
        if (l_base_layout != null) {
            l_base_layout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            setTitleWidget(l_base_layout);
            unbinder = ButterKnife.bind(this);
        }
    }

    private void setTitleWidget(View v) {
        l_title = (LinearLayout) v.findViewById(R.id.n_title);
        n_tv_title = (TextView) v.findViewById(R.id.n_tv_title);
        n_left_title = (TextView) v.findViewById(R.id.n_left_title);
        back = (LinearLayout) v.findViewById(R.id.n_back);
        n_action = (ImageView) v.findViewById(R.id.n_action);
        net_less_layout = (LinearLayout) v.findViewById(R.id.net_less_layout);
        data_less_layout = (LinearLayout) v.findViewById(R.id.data_less_layout);
    }

    private void initWindow() {
        // 默认全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        // 不全屏显示
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        // 全屏显示
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        customDestroy();
        unbinder.unbind();
        if (myReceiver != null) {   //判断广播是否注册
            this.unregisterReceiver(myReceiver);   //注销广播
        }

        MyApplication.removeFromStackList(this);
    }

    //显示隐藏标题栏目
    protected void setShowTitle(boolean isShow) {
        if (isShow) {
            l_title.setVisibility(View.VISIBLE);
        } else {
            l_title.setVisibility(View.GONE);
        }
    }


    //设置标题文字
    protected void setTitle(String title) {
        n_tv_title.setText(title);
    }

    //设置标题文字
    protected void setLeftTitle(String title) {
        n_left_title.setText(title);
    }

    //得到返回按钮并设置旁边的按钮文字
    protected LinearLayout setBackAndLeftTitle(String title) {
        back.setVisibility(View.VISIBLE);
        if (title.isEmpty()) {
            n_left_title.setVisibility(View.GONE);
        } else {
            n_left_title.setVisibility(View.VISIBLE);
            n_left_title.setText(title);
        }
        return back;
    }

    //得到功能按钮
    protected ImageView setActionBtn(@DrawableRes int Res) {
        n_action.setVisibility(View.VISIBLE);
        n_action.setImageDrawable(getResources().getDrawable(Res));
        return n_action;
    }


    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 联网请求
     */
    protected abstract void initNetData();

    /**
     * 自定义销毁
     */
    protected abstract void customDestroy();

    //隐藏状态栏
    void hideStatusBar() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
    }

    //显示状态栏
    void showStatusBar() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
    }

    protected void showToast(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }


    protected void StartActivityByIntent(Intent intent, Class<?> cls) {

        intent.setClass(this, cls);
        startActivity(intent);

    }

    protected void StartActivityByNoIntent(Class<?> cls) {


        startActivity(new Intent(this, cls));


    }

    public static Context getContext() {
        return context;
    }

    public static Activity getActivity() {
        return activity;
    }

}
